﻿@using Telerik.Web.Mvc.UI;
@model ManufacturerModel

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)
@Html.SmartStore().TabStrip().Name("manufacturer-edit").Style(TabsStyle.Material).Items(x =>
{
    x.Add().Text(T("Admin.Catalog.Manufacturers.Info").Text).Content(TabInfo()).Selected(true);
    x.Add().Text(T("Admin.Common.SEO").Text).Content(TabSeo());
    x.Add().Text(T("Admin.Catalog.Manufacturers.Products").Text).Content(TabProducts());

    EngineContext.Current.Resolve<IEventPublisher>().Publish(new TabStripCreated(x, "manufacturer-edit", this.Html, this.Model));
})
@helper TabInfo()
{ 
	<table class="adminContent mb-2">
	    @if (Model.Id != 0)
	    {
		    <tr>
			    <td class="adminTitle">
				    @Html.SmartLabelFor(model => model.Id)
			    </td>
			    <td class="adminData">
				    @Html.TextBoxFor(model => model.Id, new { @readonly = "readonly", @class = "form-control-plaintext" })
				    @Html.ValidationMessageFor(model => model.Id)
			    </td>
		    </tr>
	    }
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.Published)
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.Published)
                @Html.ValidationMessageFor(model => model.Published)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.DisplayOrder)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DisplayOrder)
                @Html.ValidationMessageFor(model => model.DisplayOrder)
            </td>
        </tr>
	</table>

    @(Html.LocalizedEditor<ManufacturerModel, ManufacturerLocalizedModel>("manufacturer-info-localized",
        @<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].Name)
                </td>
                <td class="adminData">
					@*IMPORTANT: Do not delete, this hidden element contains the id to assign localized values to the corresponding language *@
					@Html.HiddenFor(model => model.Locales[item].LanguageId)

                    @Html.EditorFor(model => Model.Locales[item].Name)
                    @Html.ValidationMessageFor(model => model.Locales[item].Name)
                </td>
            </tr>
            <tr class="description-container">
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].Description)
                </td>
                <td class="adminData wide">
                    @Html.EditorFor(model => model.Locales[item].Description, "Html")
                    @Html.ValidationMessageFor(model => model.Locales[item].Description)
                </td>
            </tr>
            <tr class="description-container d-none">
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].BottomDescription)
                </td>
                <td class="adminData wide">
                    @Html.EditorFor(model => model.Locales[item].BottomDescription, "Html")
                    @Html.ValidationMessageFor(model => model.Locales[item].BottomDescription)
                </td>
            </tr>
        </table>
        ,
        @<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Name)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.Name)
                    @Html.ValidationMessageFor(model => model.Name)
                </td>
            </tr>
            <tr class="description-container">
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Description)
                </td>
                <td class="adminData wide">
                    @Html.EditorFor(x => x.Description, "Html")
                    @Html.ValidationMessageFor(model => model.Description)
                </td>
            </tr>
            <tr class="description-container d-none">
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.BottomDescription)
                </td>
                <td class="adminData wide">
                    @Html.EditorFor(x => x.BottomDescription, "Html")
                    @Html.ValidationMessageFor(model => model.BottomDescription)
                </td>
            </tr>
			<tr>
				<td class="adminTitle"></td>
				<td class="adminData">
					<button type="button" class="btn btn-secondary description-toggle-button">
						<i class="fa fa-exchange-alt"></i>
						<span>@T("Admin.Catalog.Categories.DescriptionToggle")</span>
					</button>
				</td>
			</tr>
        </table>
    ))

    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.PictureId)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.PictureId, new { transientUpload = true })
                @Html.ValidationMessageFor(model => model.PictureId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.AllowCustomersToSelectPageSize)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.AllowCustomersToSelectPageSize)
                @Html.ValidationMessageFor(model => model.AllowCustomersToSelectPageSize)
            </td>
        </tr>
        <tr id="pnlPageSize">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.PageSize)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.PageSize)
                @Html.ValidationMessageFor(model => model.PageSize)
            </td>
        </tr>
        <tr id="pnlPageSizeOptions">
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.PageSizeOptions)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.PageSizeOptions)
                @Html.ValidationMessageFor(model => model.PageSizeOptions)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.ManufacturerTemplateId)
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.ManufacturerTemplateId, Model.AvailableManufacturerTemplates)
                @Html.ValidationMessageFor(model => model.ManufacturerTemplateId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SelectedDiscountIds)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SelectedDiscountIds)
                @Html.ValidationMessageFor(model => model.SelectedDiscountIds)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SelectedStoreIds)
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.SelectedStoreIds)
                @Html.ValidationMessageFor(model => model.SelectedStoreIds)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.SmartLabelFor(model => model.SelectedCustomerRoleIds)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SelectedCustomerRoleIds)
                @Html.ValidationMessageFor(model => model.SelectedCustomerRoleIds)
            </td>
        </tr>
		@if(Model.CreatedOn.HasValue)
		{
			<tr>
				<td class="adminTitle">
					@Html.SmartLabelFor(model => model.CreatedOn)
				</td>
				<td class="adminData">
					<div class="form-control-plaintext">
						@Html.DisplayFor(model => model.CreatedOn)
					</div>
				</td>
			</tr>
		}
		@if(Model.UpdatedOn.HasValue)
		{
			<tr>
				<td class="adminTitle">
					@Html.SmartLabelFor(model => model.UpdatedOn)
				</td>
				<td class="adminData">
					<div class="form-control-plaintext">
						@Html.DisplayFor(model => model.UpdatedOn)
					</div>
				</td>
			</tr>
		}
    </table>
}
@helper TabSeo()
{
    @(Html.LocalizedEditor<ManufacturerModel, ManufacturerLocalizedModel>("manufacturer-seo-localized",
        @<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].MetaKeywords)
                </td>
                <td class="adminData">
					@*IMPORTANT: Do not delete, this hidden element contains the id to assign localized values to the corresponding language *@
					@Html.HiddenFor(model => model.Locales[item].LanguageId)

                    @Html.EditorFor(model => model.Locales[item].MetaKeywords)
                    @Html.ValidationMessageFor(model => model.Locales[item].MetaKeywords)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].MetaDescription)
                </td>
                <td class="adminData">
                    @Html.TextAreaFor(model => model.Locales[item].MetaDescription)
                    @Html.ValidationMessageFor(model => model.Locales[item].MetaDescription)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].MetaTitle)
                </td>
                <td class="adminData">
                    @Html.TextAreaFor(model => model.Locales[item].MetaTitle)
                    @Html.ValidationMessageFor(model => model.Locales[item].MetaTitle)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.Locales[item].SeName)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.Locales[item].SeName)
                    @Html.ValidationMessageFor(model => model.Locales[item].SeName)
                </td>
            </tr>
        </table>
        ,
        @<table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.MetaKeywords)
                </td>
                <td class="adminData">
                    @Html.EditorFor(x => x.MetaKeywords)
                    @Html.ValidationMessageFor(model => model.MetaKeywords)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.MetaDescription)
                </td>
                <td class="adminData">
                    @Html.TextAreaFor(x => x.MetaDescription)
                    @Html.ValidationMessageFor(model => model.MetaDescription)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.MetaTitle)
                </td>
                <td class="adminData">
                    @Html.TextAreaFor(x => x.MetaTitle)
                    @Html.ValidationMessageFor(model => model.MetaTitle)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.SmartLabelFor(model => model.SeName)
                </td>
                <td class="adminData">
                    @Html.EditorFor(x => x.SeName)
					@if (Model.Id != 0)
                    {
                        @Html.Action("NamesPerEntity", "UrlRecord", new { entityName = "Manufacturer", entityId = Model.Id })
                    }
                    @Html.ValidationMessageFor(model => model.SeName)
                </td>
            </tr>
        </table>
    )) 
}

@helper TabProducts()
{
    if (Model.Id > 0)
    {
		<div>
			@(Html.Telerik().Grid<ManufacturerModel.ManufacturerProductModel>()
				.Name("products-grid")
				.DataKeys(keys =>
				{
					keys.Add(x => x.Id);
				})
				.DataBinding(dataBinding =>
				{
					dataBinding.Ajax()
						.Select("ProductList", "Manufacturer", new { manufacturerId = Model.Id })
						.Update("ProductUpdate", "Manufacturer")
						.Delete("ProductDelete", "Manufacturer");
				})
				.Columns(columns =>
				{
					columns.Bound(x => x.ProductName)
						.ReadOnly()
						.Width("40%")
						.ClientTemplate(@Html.LabeledProductName("ProductId", "ProductName"));
					columns.Bound(x => x.Sku)
						.ReadOnly()
						.Width("15%");
					columns.Bound(x => x.Published)
						.ReadOnly()
						.Width("10%")
						.ClientTemplate(@Html.SymbolForBool("Published"))
						.Centered();
					columns.Bound(x => x.IsFeaturedProduct)
						.Width("10%")
						.ClientTemplate(@Html.SymbolForBool("IsFeaturedProduct"))
						.Centered();
					columns.Bound(x => x.DisplayOrder1)
						.Width("10%")
						.Centered();
					columns.Command(commands =>
					{
						commands.Edit().Localize(T);
						commands.Delete().Localize(T);
					})
					.HtmlAttributes(new { align = "right" })
					.Width("15%");
				})
				.Pageable(settings => settings.PageSize(Model.GridPageSize).Position(GridPagerPosition.Both))
				.PreserveGridState()
				.ToolBar(commands => commands.Template(ManufacturerGridCommands))
				.EnableCustomBinding(true))
		</div>
    }
    else
    {
		@T("Admin.Catalog.Manufacturers.Products.SaveBeforeEdit")
    }
}

@helper ManufacturerGridCommands(Grid<ManufacturerModel.ManufacturerProductModel> grid)
{
	@(Html.SmartStore().EntityPicker()
		.IconCssClass("fa fa-plus")
		.HtmlAttribute("class", "btn btn-warning")
		.DialogTitle(T("Admin.Catalog.Manufacturers.Products.AddNew").Text.EncodeJsString('\'', false))
		.Caption(T("Admin.Catalog.Manufacturers.Products.AddNew").Text.EncodeJsString('\'', false))
		.OnSelectionCompleted("PickProducts_Completed"))
}

<script type="text/javascript">
	$(function () {
        $("#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)").change(togglePageSize);
        togglePageSize();

		// Toggle top\bottom description.
		$('.description-toggle-button').click(function() {
			$('.description-container').toggleClass('d-none');
		});
    });

	function PickProducts_Completed(ids) {
		$({}).doAjax({
			type: 'POST',
			url: '@Url.Action("ProductAdd")',
			data: { "manufacturerId": @Model.Id, "selectedProductIds": ids },
			callbackSuccess: function () {
				$('#products-grid').data('tGrid').ajaxRequest();
			}
		});
		return true;
	}

    function togglePageSize() {
        var isEnabled = $('#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)').val();

        if (isEnabled != "false") {
            $('#pnlPageSize').hide();
            $('#pnlPageSizeOptions').show();
        } else {
            $('#pnlPageSize').show();
            $('#pnlPageSizeOptions').hide();
        }
    }
</script>
